
import javax.swing.*;
import java.awt.image.*;
import java.awt.event.*;
import java.awt.*;

//public class GamePanel extends JPanel implements Runnable
public class GamePanel extends JPanel implements Runnable 
{
	
  private static final int PWIDTH = 384;   // size of panel
  private static final int PHEIGHT = 384; 
  // off-screen rendering
  private Graphics dbg; 
  private Image dbImage = null;
  Thread 		GameLoop;
  
  ImageLoader Images;
  
  public void run()
  /* The frames of the animation are drawn inside the while loop. */
  {

	  while( true ){
		  // Attempt to create a Graphics 
		  if (dbImage == null){
		  	dbImage = createImage(PWIDTH, PHEIGHT);
		  	if (dbImage != null) 
		  		dbg = dbImage.getGraphics();
		  	else
                try{
                	Thread.sleep(1000L);  // nano -> ms
			    }
			  	catch(InterruptedException ex){}
    	  }
		  if( dbg != null ){
		  
			  dbg.drawImage(Images.image , 0, 0, this);
			  try{
				  	Thread.sleep(1000L);  // nano -> ms
			  }
			  catch(InterruptedException ex){}
	
			  // Paint screen
			  try {
				  Graphics g;
				  g = this.getGraphics();
				  if ((g != null) && (dbImage != null))
					  g.drawImage(dbImage, 0, 0, null);
				  // Sync the display on some systems.
				  // (on Linux, this fixes event queue problems)
				  Toolkit.getDefaultToolkit().sync();

				  g.dispose();
			  }
			  catch (Exception e)
			  { System.out.println("Graphics context error: " + e);  }
		  }
	  } // end of run()
  } // end of run()
  
	public GamePanel()
	{
		setDoubleBuffered(false);
		setBackground(Color.black);
		setPreferredSize( new Dimension(PWIDTH, PHEIGHT));

		setFocusable(true);
		requestFocus();    // the JPanel now has focus, so receives key events

		Images = new ImageLoader();
		Images.LoadImages();
		
		GameLoop = new Thread(this);
		GameLoop.start();
	}

  //private void paintScreen()
  //// use active rendering to put the buffered image on-screen
  //{ 
  //  Graphics g;
  //  try {
  //    g = this.getGraphics();
  //    if ((g != null) && (dbImage != null))
  //      g.drawImage(dbImage, 0, 0, null);
  //    // Sync the display on some systems.
  //    // (on Linux, this fixes event queue problems)
  //    Toolkit.getDefaultToolkit().sync();

  //    g.dispose();
  //  }
  //  catch (Exception e)
  //  { System.out.println("Graphics context error: " + e);  }
  //} // end of paintScreen()
}  // end of GamePanel class

